package com.power.bank.web.controller.member;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.power.bank.common.core.domain.AjaxResult;
import com.power.bank.common.core.page.TableDataInfo;
import com.power.bank.common.enums.BusinessType;
import com.power.bank.common.utils.StringUtils;
import com.power.bank.common.utils.poi.ExcelUtil;
import com.power.bank.core.controller.BaseController;
import com.power.bank.member.domain.PowerBank;
import com.power.bank.utils.AliyunSendMessageUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.power.bank.common.annotation.Log;
import com.power.bank.member.service.IPowerBankService;

/**
 * memebrController
 *
 * @author power
 * @date 2024-12-18
 */
@RestController
@RequestMapping("/member/device")
public class PowerBankController extends BaseController {
    @Autowired
    private IPowerBankService powerBankService;

    /**
     * 查询memebr列表
     */
    @PreAuthorize("@ss.hasPermi('member:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(PowerBank powerBank) {
        startPage();
        List<PowerBank> list = powerBankService.selectPowerBankList(powerBank);
        return getDataTable(list);
    }

    /**
     * 导出memebr列表
     */
    @PreAuthorize("@ss.hasPermi('memebr:device:export')")
    @Log(title = "memebr", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PowerBank powerBank) {
        List<PowerBank> list = powerBankService.selectPowerBankList(powerBank);
        ExcelUtil<PowerBank> util = new ExcelUtil<PowerBank>(PowerBank.class);
        util.exportExcel(response, list, "powerBank数据");
    }

    /**
     * 获取memebr详细信息
     */
    @PreAuthorize("@ss.hasPermi('memebr:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(powerBankService.selectPowerBankById(id));
    }

    /**
     * 新增memebr
     */
    @PreAuthorize("@ss.hasPermi('memebr:device:add')")
    @Log(title = "memebr", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PowerBank powerBank) {
        return toAjax(powerBankService.insertPowerBank(powerBank));
    }

    /**
     * 修改memebr
     */
    @PreAuthorize("@ss.hasPermi('memebr:device:edit')")
    @Log(title = "memebr", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PowerBank powerBank) {
        return toAjax(powerBankService.updatePowerBank(powerBank));
    }

    @Autowired
    AliyunSendMessageUtils aliyunSendMessageUtils;

    /**
     * 同步
     */
    @PreAuthorize("@ss.hasPermi('memebr:device:edit')")
    @Log(title = "memebr", businessType = BusinessType.UPDATE)
    @PutMapping("/syanc")
    public AjaxResult syanc(@RequestBody PowerBank powerBank) {
        powerBank.setShopName(powerBank.getShopName().replace("(", ""));
        powerBank.setShopName(powerBank.getShopName().replace(")", ""));
        String iotId = aliyunSendMessageUtils.registerDrive(powerBank.getShopName(), "k20xwqdHtU3");
        AjaxResult ajaxResult = AjaxResult.success();
        if (StringUtils.isNotEmpty(iotId)) {
            powerBank.setIotId(iotId);
            powerBank.setSyancStatus("同步成功");
            powerBankService.updatePowerBank(powerBank);
        } else {
            ajaxResult = AjaxResult.error("同步失败");
            powerBank.setSyancStatus("同步失败");
        }
        return ajaxResult;
    }

    /**
     * 删除memebr
     */
    @PreAuthorize("@ss.hasPermi('memebr:device:remove')")
    @Log(title = "memebr", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(powerBankService.deletePowerBankByIds(ids));
    }
}
